package live.jialing.sip.contact.employee.service;

import com.google.common.collect.Lists;
import live.jialing.core.data.LinkType;
import live.jialing.core.data.jpa.CustomJpaRepository;
import live.jialing.core.service.BaseDomainService;
import live.jialing.core.service.GeneralService;
import live.jialing.data.util.SpecificationUtil;
import live.jialing.sip.contact.employee.domain.EmployeeGroup;
import live.jialing.sip.contact.employee.repository.EmployeeGroupRepository;
import live.jialing.structure.domain.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EmployeeContactListService extends BaseDomainService<EmployeeGroup, Long> implements GeneralService<EmployeeGroup, Long> {

    @Resource
    private EmployeeGroupRepository employeeGroupRepository;

    @Override
    public CustomJpaRepository getRepository() {
        return this.employeeGroupRepository;
    }

    public Specification<EmployeeGroup> buildSpecification(Class<EmployeeGroup> classz, Map<String, Object> searchParams, LinkType type) {

        return SpecificationUtil.buildSpecification(classz, searchParams, type);
    }

    /**
     * 获取组内的学生
     *
     * @param groupId
     * @return
     */
    public List<Employee> findGroupEmployeeByGroupId(long groupId) {

        if (groupId < 1) {
            return Lists.newArrayList();
        }

        EmployeeGroup employeeGroup = employeeGroupRepository.findGroupFetchEmployeeByPK(groupId);
        if (employeeGroup == null) {
            return Lists.newArrayList();
        }

        return employeeGroup.getEmployees();
    }

}
